package com.harmontronics.hanma.paas.paas.domain.app.entity;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.harmontronics.hanma.paas.paas.consts.BaseI18nKeyEnum;
import com.harmontronics.hanma.paas.paas.consts.HttpMethodEnum;
import com.harmontronics.hanma.paas.paas.domain.app.entity.valueobject.BodyTypeEnum;
import com.harmontronics.hanma.paas.paas.domain.app.entity.valueobject.BusinessServiceTypeEnum;
import com.harmontronics.hanma.paas.paas.domain.app.entity.valueobject.HttpRequestDefinitionTypeEnum;
import com.harmontronics.hanma.paas.paas.exception.ErrorKeyException;
import lombok.Data;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * 请求定义
 *
 * @author paas-generator
 * @email paas-generator@harmontronics.com
 * @date 2021-05-18 10:20:57
 */
@Data
public class HmHttpRequestDefinition {

    /**
     * $column.comments
     */
    private Long id;
    /**
     * 请求类型
     */
    private HttpRequestDefinitionTypeEnum type;
    /**
     * 请求key
     */
    private String key;
    /**
     * 请求名称
     */
    private String name;
    /**
     * 说明
     */
    private String remark;
    /**
     * http请求method
     */
    private HttpMethodEnum method;
    /**
     * 请求路径
     */
    private String path;
    /**
     * 请求头
     */
    private String header;
    /**
     * 请求体
     */
    private String body;

    private BodyTypeEnum bodyType;
    /**
     * 查询值
     */
    private String query;
    /**
     * 返回值
     */
    private String response;
    /**
     * 创建人
     */
    private String createBy;
    /**
     * 创建时间
     */
    private LocalDateTime createDate;
    /**
     * 修改人
     */
    private String modifyBy;
    /**
     * 修改时间
     */
    private LocalDateTime modifyDate;

    /*
     *模型ID
     */
    private Long modelId;


    /**
     * 服务类型
     */
    private BusinessServiceTypeEnum serviceType;

    /**
     * 服务方式  仅正对脚本
     */
    private String serviceWay;

    /**
     * 脚本ID
     */
    private Long scriptId;



    public static List<Map<String, String>> json2List(String json) {
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            return objectMapper.readValue(json, new TypeReference<List<Map<String, String>>>() {
            });
        } catch (JsonProcessingException ex) {
            throw new ErrorKeyException(BaseI18nKeyEnum.REQUEST_PARAM_ERROR);
        }
    }

}
